package cn.tedu.view;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Random;

@WebServlet(name = "/DataView")
public class DataView extends HttpServlet {
    private ComboPooledDataSource c3p0 = new ComboPooledDataSource();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setHeader("Access-Control-Allow-Origin","*");
//        // 每次请求后，向页面发送一组随机数据
//        String result="[";
//        for (int i = 0; i < 4; i++) {
//            int num = new Random().nextInt(100);
//            result= result+num+",";
//        }
//        int num = new Random().nextInt(100);
//        result=result+num+"]";
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = c3p0.getConnection();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String todayTime = sdf.format(System.currentTimeMillis());
            // 解析横轴坐标所需的时间数据，分钟：秒的形式
            SimpleDateFormat sdf02 = new SimpleDateFormat("mm:ss");
            String xtime = sdf02.format(System.currentTimeMillis());
            ps = conn.prepareStatement("select * from tongji2 where reporttime=?");
            ps.setString(1,todayTime);
            // 执行查询并返回字符集
            rs = ps.executeQuery();
            if (0==1){
//            if (rs.next()){
                // 当天已经有数据
                int pv = rs.getInt("pv");
                int uv = rs.getInt("uv");
                int vv = rs.getInt("vv");
                int newip = rs.getInt("newip");
                int newcust = rs.getInt("newcust");
                String result = "["+pv+","+uv+","+","+vv+","+newip+","+newcust+",'"+xtime+"']";
                response.getWriter().write(result);
            }else {
                // 当天还没有数据
                String result="[0,0,0,0,0'"+xtime+"']";
                response.getWriter().write(result);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (rs!=null){
                try {
                    rs.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (ps!=null){
                try {
                    ps.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (conn!=null){
                try {
                    conn.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
    }
}
